package controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringEscapeUtils;

import util.URLHandler;

/**
 * @author yangjingyi Date 2015-10-9
 * 
 */
public class LofterNovelGrab {
	private static String filePath = "D:/Harry Potter and the Methods of rationality.txt"; // 本地保存地址
	private static String mainUrl = "http://hpmor.lofter.com/contents"; // 主页地址
	private static String charset = "utf-8"; // 网页编码
	private static Pattern chapterPtn = Pattern
			.compile(
					"<p><a\\s*target=\"_blank\"\\s*href=\"(.+?)\"\\s*>(.+?)</a>\\s*</p>\\s*",
					Pattern.DOTALL); // 章节名称与地址的正则
	private static Pattern contentPtn = Pattern.compile(
			"<div\\s*class=\"txtcont\">\\s*(.+?)\\s*</div>", Pattern.DOTALL);	//章节正文正则

	public static void main(String[] args) throws IOException {
		FileOutputStream fileOut = new FileOutputStream(filePath);
		PrintStream p = new PrintStream(fileOut);
		int i = 0;

		String mainPage = URLHandler.getURLContext(mainUrl, charset);
		mainPage = mainPage.replaceAll("&nbsp;|<br\\s*/>", " "); // 清除章节标题中的空格

		Matcher m = chapterPtn.matcher(mainPage);
		while (m.find()) {
			p.println(m.group(2));
			System.out.println(i++ + ".chap : " + m.group(1) + " | "
					+ m.group(2));

			String content = URLHandler.getURLContext(m.group(1), charset);
			Matcher mc = contentPtn.matcher(content);
			if (mc.find()) {
				String text = mc.group(1);
				text = StringEscapeUtils.unescapeHtml4(text);	//清除正文的HTML标签
				text = text.replaceAll("<p>|</p>|<br\\s*/>", "\n");
				text = text.replaceAll("<.+?>|</.+?>", " ");
				p.println(text);	//存 入文件
			}
		}

		try {
			fileOut.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		p.close();
	}
}
